
var CategoryMenu = new Class({
    Implements: [Events, Options],
        options: {
        'tagid': null
    },
    cats:[],
    initialize: function(options) {
        this.setOptions(options);
        if(!this.options.tagid) return;
    },
    setMenuData: function(categories) {
        if(!categories) return;

        this.cats = categories;
        this.options.tagid.empty();

        var ul = new Element('ul'), isGlobal;
        this.cats.each(function(cat) {
            if(isGlobal != cat.cid) {
                isGlobal = cat.cid;

                var subcat = this.getSubcategiry(cat.cid);

                new Element('li.gct[html=' + cat.gcnm + ' <tt>(' + subcat.cnt + ')</tt>]', {
                    'events': {
                        'click': function() {
                            this.getNext().setStyle('display', this.getNext().getStyle('display') == 'none' ? '' : 'none');
                        },
                        'contextmenu': function(ev) {
                            ev.stop();

                            new ShowBox().getDiscount(ev);
                        },
                        'focus': function() {this.blur();}
                    }
                }).store('cid', cat.cid).inject(ul);

                ul.adopt(subcat.ultag);

            } // if

        }, this);
        this.options.tagid.adopt(ul);

    },
    getSubcategiry: function(cid) {

        var ul = new Element('ul[style=display:none;]'), cnt = 0;
        this.cats.each(function(scat) {
            if(cid == scat.cid) {
                cnt += scat.cnt;

                new Element('li.sct[html=' + scat.scnm + ' <tt>(' + scat.cnt + ')</tt>]', {
                    'events': {
                        'click': function(ev) {
                            ev.target.setStyle('color', 'orangered');

                            this.fireEvent('newcategory', scat.sid);
                        }.bind(this),
                        'contextmenu': function(ev) {
                            ev.stop();

                            new ShowBox().getDiscount(ev);
                        },
                        'focus': function() {this.blur();}
                    }
                }).store('cid', scat.sid).inject(ul);
            }
        }, this);
        return {ultag: ul, cnt: cnt};
    }
});
